﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Edge.Business.Domain.Application;
using Edge.Common;
using Edge.Common.ComponentModel.Parameters;
using Edge.Common.Services;
using Edge.Common.Utils;
using Shop.Business.Domain.Catalog;
using Shop.Business.Domain.Catalog.Parameter;
using Shop.Business.Domain.Customers;
using SortDirection=Edge.Common.SortDirection;

namespace Shop.Web.Controls
{
    public partial class CatalogFilter : System.Web.UI.UserControl
    {
        protected ProductGroupFilter filter = new ProductGroupFilter();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IOrderedEnumerable<CatalogMenuItem> items =
                    Locator.GetService<IMenuItemManager>().Find(FetchOptions.All,
                                                                new MenuItemFilter() {Query = "brandId"}).
                        OrderByDescending(item => item.Query);
                //            IOrderedEnumerable<CatalogMenuItem> items = Locator.GetService<IMenuItemManager>().FindAll().OrderByDescending(item => item.Query);
                SortedList<int, string> brands = new SortedList<int, string>();
                //           SortedList<int, string> cats = new SortedList<int, string>();
                foreach (CatalogMenuItem item in items)
                {
                    //                if (item.Query == "catId" && !StringUtils.IsBlankOrNull(item.Parent))
                    //                {
                    //                    cats.Add(item.Id, item.Name); 
                    //                }
                    //                else 
                    if (item.Query == "brandId")
                    {
                        brands.Add(item.Id, item.Name);
                    }
                }
                ctrlBrandItem.DataSourse = brands;
                //ctrlCatItem.DataSourse = cats;
                SortedList<int, string> list2 = new SortedList<int, string>();
                ParameterValueFilter parameterValueFilter = new ParameterValueFilter();
                parameterValueFilter.Parameter = Locator.GetService<IParameterManager>().Load(1);
                parameterValueFilter.AtMenu = true;
                FetchOptions options = FetchOptions.All;
                options.SortExpression = "OrderOf";
                options.SortDirection = SortDirection.Descending;
                var sizeDs = Locator.GetService<IParameterValueManager>().Find(options, parameterValueFilter);
                foreach (var size in sizeDs)
                {
                    list2.Add(Convert.ToInt32(size.Value), size.Value);
                }
                ctrlSizeItem.DataSourse = list2;
                chbIsBrandArt.Visible = Locator.GetService<IContextObjects>().Get<CustomerHolder>() != null &&
                                        Locator.GetService<IContextObjects>().Get<CustomerHolder>().Object.Role !=
                                        CustomerRole.Buyer;
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            WebParameterAttribute.SetValues(filter, Request);
            string s = "Catalog.aspx?"+WebParameterAttribute.GetQueryString(filter).Substring(1);
            chbIs.Checked = filter.InStock;
            ctrlBrandItem.StartValue = s;
            ctrlBrandItem.QueryType = "brandList";
            //ctrlCatItem.StartValue = filter.CatIds;
            tbKeyword.Text = filter.Keyword;
            chbIsBrandArt.Checked = filter.IsBrandArt;
            ctrlSizeItem.StartValue = s;
            ctrlSizeItem.QueryType = "sizeList";
        }

        protected void btnCheck_OnClick(object sender, EventArgs e)
        {
            WebParameterAttribute.SetValues(filter, Request);
            filter.InStock = chbIs.Checked;
            //filter.BrandIds = ((HiddenField) ctrlBrandItem.FindControl("hdnValues")).Value;
            //filter.CatIds = ((HiddenField) ctrlCatItem.FindControl("hdnValues")).Value;
            filter.Keyword = tbKeyword.Text;
            filter.IsBrandArt = chbIsBrandArt.Checked;
            //filter.SizeIds = ((HiddenField) ctrlSizeItem.FindControl("hdnValues")).Value;
            string s = WebParameterAttribute.GetQueryString(filter).Substring(1);
            Response.Redirect("~/Catalog.aspx?"+s,true);
            
        }
    }
}